package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.view.WheelView;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.lxj.xpopup.core.BottomPopupView;

public class BottomTimePeriodDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private LinearLayout undisturbedLayout;
    private TextView tvLabel;
    private SwitchButton btnUndisturbedSwitch;
    private WheelView wv_first;
    private WheelView wv_second;
    private WheelView wv_third;
    private WheelView wv_four;
    private NumericWheelAdapter mFirstNumericWheelAdapter;
    private NumericWheelAdapter mSecondNumericWheelAdapter;
    private NumericWheelAdapter mThirdNumericWheelAdapter;
    private NumericWheelAdapter mFourNumericWheelAdapter;
    private int startH;
    private int startM;
    private int endH;
    private int endM;
    private String label;
    private boolean isOpen;
    private String showType = "1";

    public BottomTimePeriodDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_wheel_view_period;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        undisturbedLayout = findViewById(R.id.undisturbed_layout);
        tvLabel = findViewById(R.id.tv_label);
        btnUndisturbedSwitch = findViewById(R.id.btn_undisturbed_switch);
        wv_first = findViewById(R.id.options1);
        wv_second = findViewById(R.id.options2);
        wv_third = findViewById(R.id.options3);
        wv_four = findViewById(R.id.options4);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    String start = wv_first.getAdapter().getItem(wv_first.getCurrentItem()) + ":" + wv_second.getAdapter().getItem(wv_second.getCurrentItem());
                    String end = wv_third.getAdapter().getItem(wv_third.getCurrentItem()) + ":" + wv_four.getAdapter().getItem(wv_four.getCurrentItem());
                    mDialogClickListener.onConfirmClick(btnUndisturbedSwitch.isChecked(), wv_first.getAdapter().getItem(wv_first.getCurrentItem()) +"", wv_second.getAdapter().getItem(wv_second.getCurrentItem()) + "",wv_third.getAdapter().getItem(wv_third.getCurrentItem()) + "", wv_four.getAdapter().getItem(wv_four.getCurrentItem()) + "");
                }
            }
        });
        if(!TextUtils.isEmpty(label)){
            tvLabel.setText(label);
        }
        btnUndisturbedSwitch.setChecked(isOpen);
        setStyle();
        initWheelView();
        if(showType.equals("1")){
            undisturbedLayout.setVisibility(VISIBLE);
        }else if(showType.equals("2")){
            undisturbedLayout.setVisibility(GONE);
        }
    }

    public void setLabel(String text){
        this.label = text;
        if(tvLabel != null){
            tvLabel.setText(text);
        }
    }

    public void setSelectedTime(boolean isOpen, String start, String end){
        this.isOpen = isOpen;
        if(btnUndisturbedSwitch != null){
            btnUndisturbedSwitch.setChecked(isOpen);
        }
        String[] startHM = start.split(":");
        String[] endHM = end.split(":");
        startH = Integer.valueOf(startHM[0]);
        startM = Integer.valueOf(startHM[1]);
        endH = Integer.valueOf(endHM[0]);
        endM = Integer.valueOf(endHM[1]);
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(true);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(true);
        wv_second.setAlphaGradient(true);
//        wv_second.setLabelOffset(5);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_third.setGravity(Gravity.CENTER);
        wv_third.setCyclic(true);
        wv_third.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_third.setItemsVisibleCount(5);
        wv_third.setDividerColor(0x00000000);
        wv_third.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_third.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_four.setGravity(Gravity.CENTER);
        wv_four.setCyclic(true);
        wv_four.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_four.setItemsVisibleCount(5);
        wv_four.setDividerColor(0x00000000);
        wv_four.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_four.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        if(wv_first == null || wv_second == null || wv_third == null || wv_four == null){
            return;
        }
        mFirstNumericWheelAdapter  = new NumericWheelAdapter(0, 23);
        wv_first.setAdapter(mFirstNumericWheelAdapter);
        wv_first.setCurrentItem(startH);

        mSecondNumericWheelAdapter  = new NumericWheelAdapter(0, 59);
        wv_second.setAdapter(mSecondNumericWheelAdapter);
        wv_second.setCurrentItem(startM);

        mThirdNumericWheelAdapter  = new NumericWheelAdapter(0, 23);
        wv_third.setAdapter(mThirdNumericWheelAdapter);
        wv_third.setCurrentItem(endH);

        mFourNumericWheelAdapter  = new NumericWheelAdapter(0, 59);
        wv_four.setAdapter(mFourNumericWheelAdapter);
        wv_four.setCurrentItem(endM);
    }

    public void setShowType(String showType){
        this.showType = showType;
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(boolean isOpen, String startHour, String startMinute, String endHour, String endMinute);
    }
}
